package com.qd.pandaparkflow.compment.charge;

import com.cdqidi.exception.ApiException;
import com.qd.common.panda.domain.entity.feetime.BaseFeeTimeDTO;
import com.qd.common.panda.domain.entity.feetime.FeeTim1DTO;
import com.qd.common.panda.domain.enums.ChargeFeeParkTimeRuleEnum;
import com.qd.pandaparkflow.bean.ChargeFeeParkTimeReqVO;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.util.List;
import java.util.Objects;

/**
 * 参数验证组件
 *
 * @author sjk
 */
@ConditionalOnProperty(prefix = "liteflow", name = "enable", havingValue = "true")
@LiteflowComponent(value = "feeTimeCheckCmp")
@Slf4j
public class FeeTimeCheckComponent extends NodeComponent {

    @Override
    public void process() throws Exception {
        ChargeFeeParkTimeReqVO chargeFeeParkTimeReqVO = this.getSlot().getRequestData();
        if (null != chargeFeeParkTimeReqVO.getStartTime() && null != chargeFeeParkTimeReqVO.getEndTime()) {
            if (chargeFeeParkTimeReqVO.getEndTime().isBefore(chargeFeeParkTimeReqVO.getStartTime())) {
                throw new ApiException("结束时间不能小于开始时间");
            }
        }

        //验证计费规则ID是否存在，不存在直接报错
        ChargeFeeParkTimeRuleEnum.checkCode(chargeFeeParkTimeReqVO.getFeeTimeId());
        final List<BaseFeeTimeDTO> rules = chargeFeeParkTimeReqVO.getFeeTimeRules();
        if (null == rules || rules.isEmpty()) {
            throw new ApiException("规则内容不能为空");
        }
        final BaseFeeTimeDTO baseFeeTimeDTO = rules.get(0);
        boolean isParamCountError = false;
        if (Objects.requireNonNull(ChargeFeeParkTimeRuleEnum.getByValue(chargeFeeParkTimeReqVO.getFeeTimeId())) == ChargeFeeParkTimeRuleEnum.FEE_PARK_TIME1) {
            if (rules.size() != 1) {
                isParamCountError = true;
            }
            if (!(baseFeeTimeDTO instanceof FeeTim1DTO)) {
                isParamCountError = true;
            }
        } else {
            log.error("不支持的规则:{}", chargeFeeParkTimeReqVO.getFeeTimeId());
            throw new ApiException("不支持的规则");
        }
        if (isParamCountError) {
            log.error("billRule: {},rule:{}", chargeFeeParkTimeReqVO.getFeeTimeId(), rules);
            throw new ApiException("规则内容不正确");
        }
    }
}